package com.ktjiaoyu.crm.controller;

import com.ktjiaoyu.crm.pojo.User;
import com.ktjiaoyu.crm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/toLogin")
    public String toLogin(){

        return "login";
    }

    @RequestMapping(value = "/toMain")
    public String toMain(){

        return "main";
    }

    @PostMapping(value = "login")
    public String login(User user, HttpSession session, Model model){

        //调用查询方法，查询用户是否存在
        User loginUser = userService.login(user);
        if(loginUser!=null){
            session.setAttribute("loginUser",loginUser);
            return "main";
        }
        model.addAttribute("msg","用户名或密码错误，登录失败！");
        return "forward:/toLogin";


    }

}
